package cn.pconline.autoclub.task;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.pconline.autoclub.task.util.McUtils;


/**
 * 话题回复数处理任务 ：把话题动态的回复加到话题回复数去
 * @author zhoutianhuai
 *
 */
public class SubjectOldDataHandleTask {
  private static Logger log = LoggerFactory.getLogger(SubjectOldDataHandleTask.class);
  /**查询所有话题sql**/
  private String getAllSubjectSql = "SELECT * from ac_subject_info";
  /**计算话题动态回复总数根据话题IDsql**/
  private String sumSubjectDynaReplyCountSql = "select sum(post_count)as total_reply_count from ac_subject_dyna where subject_id = ?";
  /**更新话题内容**/
  private String updateSubjectSql = "update ac_subject_info  set reply_count = ? where subject_id = ?";
  public static void setLog(Logger log) {
	  SubjectOldDataHandleTask.log = log;
  }
  
  public static void main(String[] args) {
	  //初始化Log
      AppUtils.initLog(SubjectOldDataHandleTask.class);
      SubjectOldDataHandleTask t = new SubjectOldDataHandleTask();
      AppUtils.log.info("run subjectOldDataHandleTask start...");
      try {
    	  t.subjectOldDataHandle();
      } catch (SQLException ex) {
          AppUtils.log.error("subjectOldDataHandleTask fail!", ex);
      }
      AppUtils.log.info("subjectOldDataHandleTask done!");
  }
   
  
  /**
   * 处理所有话题旧数据的回复数
   *
   * @param petrolMap
   * @throws SQLException
   */
private void subjectOldDataHandle() throws SQLException {
      Connection conn = DBUtils.getConnection();
      boolean oldCommit = DBUtils.getAutoCommitValue(conn);
      conn.setAutoCommit(false);
      PreparedStatement ps = null;
      ResultSet rs = null;
      try {
          log.info("---->话题旧数据处理任务---->准备执行……");
          long start = System.currentTimeMillis();
          log.info("---->话题旧数据处理任务---->执行中……");
          //查找所有话题
          ps = conn.prepareStatement(getAllSubjectSql);
          rs = ps.executeQuery();
          while(rs.next()){
        	  oldDataHandle(conn, ps, rs);
          }
          
          long end = System.currentTimeMillis();
          log.info("耗时{}秒", (end - start) / 1000.0);
          log.info("---->话题旧数据处理任务---->执行完毕！");
      } catch (Exception e) {
          log.error("subjectOldDataHandle exception!", e);
      } finally {
          conn.commit();
          conn.setAutoCommit(oldCommit);
          DBUtils.releaseConn(conn, ps, rs);
      }

  }

/**
 * 旧数据处理
 * @param conn
 * @param ps
 * @param rs
 * @param isLongContent
 * @param content
 * @param activityId
 * @throws SQLException
 */
private void oldDataHandle(Connection conn, PreparedStatement ps, ResultSet rs) throws SQLException{
	long subjectId = rs.getLong("subject_id");
	int replyCount = rs.getInt("reply_count");
    String getSubjectDynaSql = DBUtils.getSql(sumSubjectDynaReplyCountSql, "ac_subject_dyna", subjectId);
	ps = conn.prepareStatement(getSubjectDynaSql);
    ps.setLong(1, subjectId);
    rs = ps.executeQuery();
    while (rs.next()) {
			int total_reply_count = rs.getInt("total_reply_count");
			if(total_reply_count == 0){
				continue;
			}    
			
			 replyCount += total_reply_count;
			 ps = conn.prepareStatement(updateSubjectSql);
             ps.setInt(1,replyCount);
             ps.setLong(2, subjectId);
             ps.executeUpdate();
             //清MC缓存
             McUtils.delete("Subject-" + subjectId);
		  }
   }
	 
}
